package cn.openea.perpartner.meily.web.controller;

import cn.hutool.core.util.StrUtil;
import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.core.web.page.TableDataInfo;
import cn.openea.perpartner.meily.web.entity.Corp;
import cn.openea.perpartner.meily.web.service.ICorpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
 * @author Weird077
 * @description TODO
 * @date 2023-05-18 17:41
 */
@Api(tags = "企业微信接口")
@RestController
@RequestMapping("/qyapi")
public class CorpController extends BaseController {

    @Autowired
    private ICorpService corpService;

    @ApiOperation(value = "客服帐号列表")
    @PostMapping("/kf/account/list")
    public TableDataInfo getAccountList() {
        List<Corp> list = corpService.getAccountList();
        return getDataTable(list);
    }

    @ApiOperation(value = "客服帐号链接")
    @GetMapping("/kf/account/url/{openKfId}")
    public AjaxResult getAccountUrl(@PathVariable String openKfId) {
        String url = corpService.getAccountUrl(openKfId);
        return StrUtil.isNotBlank(url) ? AjaxResult.success("获取客服链接成功！",url) : error("获取客服链接失败！");
    }

    @ApiOperation(value = "同步客服账号")
    @GetMapping("/kf/account/sync")
    public AjaxResult syncKfAccount() {
        return corpService.syncKfAccount() ? success("同步成功！") : error("同步失败！");
    }

    @ApiOperation(value = "删除客服账号")
    @PostMapping("/kf/account/delete")
    public AjaxResult deleteKfAccount(@RequestBody List<String> openKfIdList) {
        return corpService.removeBatchByIds(openKfIdList) ? success() : error();
    }

    @ApiOperation(value = "客服列表-小程序")
    @PostMapping("/kf/account/list/applet")
    public AjaxResult getList(@RequestBody Corp corp) {
        return AjaxResult.success("获取客服列表成功！", corpService.getList(corp));
    }

    @ApiOperation(value = "客服帐号链接-小程序")
    @PostMapping("/kf/account/url/applet")
    public AjaxResult getKFUrl(@RequestBody Corp corp) {
        if(StrUtil.isBlank(corp.getOpenKfid())){
            AjaxResult.error("获取不到客服帐号ID！");
        }
        String url = corpService.getKFUrl(corp.getOpenKfid(), corp.getName());
        return StrUtil.isNotBlank(url) ? AjaxResult.success("获取客服链接成功！",url) : error("获取客服链接失败！");
    }

}
